<?php
/**
 * 点赞|收藏
 * @return
 * @author yuncran@126.com
 */

namespace app\model;

use app\BaseModel;
use app\CacheKey;

class TrendsUserLikeModel extends BaseModel
{
    protected $pk = "id";

    protected $name = "trends_user_like";

    protected $append = ['user'];


    /**
     * 创建记录
     * @param $user_id
     * @param $trends_id
     * @param $status
     * @return TrendsUserLikeModel|\think\Model
     * @author yuncran@126.com
     */
    public static function addTrendsLike($user_id, $trends_id, $status,$to_user_id,$share_trends_id)
    {
        return self::create([
            'user_id' => $user_id,
            'to_user_id' => $to_user_id,
            'trends_id' => $trends_id,
            'status' => $status,
            'share_trends_id' => $share_trends_id,
            'create_time' => time(),
        ]);
    }


    public function getUserAttr($value, $data)
    {
        return UserModel::where(['id' => $data['user_id']])->field('id,nickname,avatar,email,level,bio,verification')->append(['level_name'])
            ->withAttr('level_name', function ($value, $data) {
                return UserLevelModel::where(['id' => $data['level']])->cache(CacheKey::level_title)->value('title');
            })->find();
    }

    /**
     * 获取是否点赞分享
     * @param $use_id
     * @return array|bool|mixed|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author yuncran@126.com
     */
    public static function getIsStatus($use_id, $status, $trends_id)
    {
        return self::getModel()->where('user_id', $use_id)->where('trends_id', $trends_id)->where('status', $status)->find();
    }

}